package com.broada.apm.es.handler;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import org.apache.wink.client.ClientResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;

import com.broada.apm.es.bean.MetadataModel;
import com.broada.apm.indicator.api.common.ApmDataIndicatorException;
import com.broada.apm.utils.EsClientUtil;
import com.broada.apm.utils.RestClientUtil;

/**
 * 
 * 数据初始化操作类
 * 
 * @author louyi Create By Dec 3, 2014 10:11:37 AM
 */

public class ApmDataImporter {
	private static Logger log = LoggerFactory.getLogger(ApmDataImporter.class);
	private ApmDataImporter(){
		
	}
	@SuppressWarnings("unchecked")
	public static void importFromYaml(File file) throws ApmDataIndicatorException {
		log.info("import data from {}",file);
		String url = EsClientUtil.getEsHttpUrl();
		Yaml yaml = new Yaml();
		List<MetadataModel> result;
		try {
			result = (List<MetadataModel>) yaml.load(new FileInputStream(file));
			for (MetadataModel metaData : result) {
				String _action = metaData.getAction();
				String _url = url + metaData.getPath();
				String _entity = metaData.getEntity();
				ClientResponse response = RestClientUtil.doAction(_action, _url, _entity);
				log.info(response.getEntity(String.class));
			}
		} catch (FileNotFoundException e) {
			throw new ApmDataIndicatorException(e);
		}
	}
}